<template>
  <div class="addAndEdit base-addAndEdit">
    <h3>用户信息</h3>
    <el-form :model="form" label-width="100px" :disabled="type != 'add'">
      <el-row>
        <el-col :span="12">
          <el-form-item label="角色名称">
            <el-input v-model="form.roleName" placeholder="请输入角色名称" />  
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="角色描述">
            <el-input v-model="form.roleDesc" placeholder="请输入角色描述" />  
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row justify="center">
      <template v-if="type == 'add'">
        <el-button type="primary" @click="handleSaveOrBack(true)"
          >保存</el-button
        >
      </template>
      <el-button @click="handleSaveOrBack(false)">返回</el-button>
    </el-row>

    <!-- 添加的时候不需要展示角色表格 -->
    <el-table
      v-if="type !== 'add'"
      :data="tableData"
      style="margin: 20px 0"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="roleName" label="权限" />
      <el-table-column prop="roleDesc" label="描述" />
    </el-table>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import Mitt from "@/mitt/index.js";

const emits = defineEmits(["sendData"]);
//从viewAll传递到的数据，用来判断当前组件的状态是添加还是编辑
const { currentShow } = defineProps(["currentShow"]);
//defineProps不支持二次解构
const { type, row } = currentShow;

//表单数据
const form = ref({
  roleName: "",
  roleDesc: "",
});
const handleSaveOrBack = (type) => {
  //如果是保存，调用保存的接口，否则直接返回
  if (type) {
    console.log(form.value);
  }

  emits("sendData", {
    showComponents: "viewAll",
    type: "",
  });
};

const tableData = [
  {
    id: 1,
    roleName: "系统用户",
    roleDesc: "系统用户",
  },

  {
    id: 3,
    roleName: "经理",
    roleDesc: "负责规划",
    children: [
      {
        id: 31,
        roleName: "查看全部产品",
        roleDesc: "order:add",
      },
      {
        id: 32,
        roleName: "修改产品",
        roleDesc: "order:add",
      },
    ],
  },
];

//表单数据回显
const getList = () => {
  //通过传递的数据id请求接口
  console.log(row);
};

onMounted(() => {
  Mitt.emit("sendcrumbsInfo", { first: "角色管理", second: "角色表单" });
  if (type != "add") {
    //如果状态不处在添加状态时，需要请求接口回显数据
    getList();
  }
});
</script>

<style lang="less" scoped></style>
